package com.atguigu.exceptionHandler;


import com.atguigu.comMonutils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e) {
        return R.error().message(e.getMessage());
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e) {
        return R.error().message(e.getMessage());
    }


    @ExceptionHandler(GuliException.class)
    @ResponseBody
    public R error(GuliException e) {
//        e里面有new传送过来的信息.
        log.error(e.getMsg());
        return R.error().message(e.getMsg()).code(e.getCode());
    }

    @ExceptionHandler(DuplicatedNameException.class)
    @ResponseBody
    public R error(DuplicatedNameException e) {
//        e里面有new传送过来的信息.
        log.error(e.getMsg());
        return R.error().message(e.getMsg()).code(e.getCode());
    }
}
